cc.Class({
    extends: cc.Component,

    properties: {
        thorn: cc.Node
    },

    runAction(node, action) {
        return new Promise(resolve => {
            node.runAction(cc.sequence(
                action,
                cc.callFunc(resolve)
            ))
        })
    },

    delay(t=0) {
        return new Promise(resolve => {
            setTimeout(resolve, t * 1000)
        })
    },

    start() {
        const action = {
            scaleUp: cc.scaleTo(.5, 1),
            scaleDown: cc.scaleTo(.5, 0),
            rotate: cc.rotateBy(.5, -45)
        }

        animate.call(this)
        function animate() {
            this.delay(2)
                .then(() => this.runAction(this.thorn, action.scaleDown))
                .then(() => this.delay(2))
                .then(() => this.runAction(this.node, action.rotate))
                .then(() => this.delay(2))
                .then(() => this.runAction(this.thorn, action.scaleUp))
                .then(() => animate.call(this))
        }
    }
})